package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {
     SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
     @Override
     public List<Brand> selectAll(){
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
          List<Brand> brands = mapper.selectAll();
          sqlSession.close();
          return brands;
     }

     @Override
     public void add(Brand brand) {
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
//          mapper.add(brand);
          mapper.add(brand);
          sqlSession.close();
     }

     @Override
     public void deleteById(int id) {
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
          mapper.deleteById(id);
          sqlSession.close();
     }

     @Override
     public Brand selectOne(int id) {
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
          Brand brand = mapper.selectOne(id);
          sqlSession.close();
          return brand;
     }

     @Override
     public void updateBrandById(Brand brand) {
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
          mapper.updateBrandById(brand);
          sqlSession.close();
     }

     @Override
     public void deleteByIds(int[] ids) {
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
          mapper.deleteByIds(ids);
          sqlSession.close();
     }

     public PageBean<Brand> selectByPage(int currentPage, int size) {
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
          int begin = (currentPage - 1) * size;
          List<Brand> brands = mapper.selectByPage(begin,size);
          int totalCount = mapper.selectTotalCount();
          PageBean<Brand> pageBean = new PageBean<>();
          pageBean.setRows(brands);
          pageBean.setTotalCount(totalCount);
          sqlSession.close();
          return pageBean;
     }

     @Override
     public PageBean<Brand> selectByPageAndCondition(int currentPage, int size, Brand brand) {
          SqlSession sqlSession = sqlSessionFactory.openSession(true);
          BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
          int begin = (currentPage - 1) * size;

          //处理brand条件，模糊表达式

          String brandName = brand.getBrandName();
          if (brandName != null && brandName.length()>0) {
               brand.setBrandName("%"+brandName+"%");
          }

          String companyName = brand.getCompanyName();
          if (companyName != null && companyName.length()>0) {
               brand.setCompanyName("%"+companyName+"%");
          }

          List<Brand> brands = mapper.selectByPageAndCondition(begin,size,brand);
          int totalCount = mapper.selectTotalCountByCondition(brand);
          PageBean<Brand> pageBean = new PageBean<>();
          pageBean.setRows(brands);
          pageBean.setTotalCount(totalCount);
          sqlSession.close();
          return pageBean;
     }


}
